package net.hvacapp.cmvrfforandroid.ui.project.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import net.hvacapp.cmvrfforandroid.R;
import net.hvacapp.cmvrfforandroid.ui.project.model.RoomData;
import net.hvacapp.cmvrfforandroid.utils.NumberUtil;
import net.hvacapp.cmvrfforandroid.utils.ViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangnorman on 15/7/29.
 */
public class RoomAdapter extends BaseAdapter {

    private Context mContext;
    private List<RoomData> mData = new ArrayList<>();

    public RoomAdapter(Context context) {
        this.mContext = context;
    }

    /**
     * 添加数据
     *
     * @param data 数据集合
     */
    public void addAll(List<RoomData> data) {
        mData.clear();
        mData.addAll(data);
        notifyDataSetChanged();
    }

    public void addOne(RoomData data) {
        mData.add(data);
        notifyDataSetChanged();
    }

    public void deleteOne(RoomData data) {
        mData.remove(data);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.two_lines_cell_item, null);
        }

        RoomData item = mData.get(position);

        ImageView image = ViewHolder.get(convertView, R.id.iv_selected_mark);
        image.setImageResource(R.drawable.user_selection_hint);
        image.setVisibility(View.VISIBLE);

        TextView titleTv = ViewHolder.get(convertView, R.id.tv_title);
        titleTv.setText(item.getName());
        TextView descTv = ViewHolder.get(convertView, R.id.tv_description);
        descTv.setText(NumberUtil.formateDouble(item.getArea()) + "平米 " + NumberUtil.formateDouble(item.getLoad()) + "W");

        return convertView;
    }


}
